package com.haibara.codesubmiteval.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haibara.codesubmiteval.annotation.AuthCheck;
import com.haibara.codesubmiteval.common.BaseResponse;
import com.haibara.codesubmiteval.common.BatchDeleteRequest;
import com.haibara.codesubmiteval.common.DeleteRequest;
import com.haibara.codesubmiteval.common.ErrorCode;
import com.haibara.codesubmiteval.constant.AuthConstant;
import com.haibara.codesubmiteval.exception.ThrowUtils;
import com.haibara.codesubmiteval.mapper.QuestionSubmitMapper;
import com.haibara.codesubmiteval.model.dto.questionSubmit.QuestionSubmitAddRequest;
import com.haibara.codesubmiteval.model.dto.questionSubmit.QuestionSubmitQueryRequest;
import com.haibara.codesubmiteval.model.dto.questionSubmit.QuestionSubmitUpdateRequest;
import com.haibara.codesubmiteval.model.dto.questionSubmit.QuestionSubmitVOQueryRequest;
import com.haibara.codesubmiteval.model.entity.QuestionSubmit;
import com.haibara.codesubmiteval.model.vo.Least5TimeSubmitVO;
import com.haibara.codesubmiteval.model.vo.QuestionSubmitVO;
import com.haibara.codesubmiteval.service.QuestionSubmitService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目提交记录信息
 *
 * @Author: mengyu
 * @Date: 2025/2/6
 */
@RestController
@RequestMapping("/question_submit")
@Slf4j
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;
    @Resource
    private QuestionSubmitMapper questionSubmitMapper;

    /**
     * 提交题目
     *
     * @param questionSubmitAddRequest 题目提交请求
     * @return 提交结果
     */
    @PostMapping("/submit")
    @AuthCheck(mustRole = AuthConstant.USER)
    public BaseResponse<Long> submit(@RequestBody @Valid QuestionSubmitAddRequest questionSubmitAddRequest) {
        Long id = questionSubmitService.doSubmit(questionSubmitAddRequest);
        return BaseResponse.success(id);
    }


    /**
     * 分页查询题目提交记录信息
     *
     * @param questionSubmitQueryRequest 分页查询请求
     * @return 对应数据
     */
    @PostMapping("/page")
    public BaseResponse<Page<QuestionSubmit>> pageQueryQuestionSubmit(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest) {
        Page<QuestionSubmit> result = questionSubmitService.pageQuestionSubmit(questionSubmitQueryRequest);
        return BaseResponse.success(result);
    }

    /**
     * 分页查询题目提交记录脱敏信息
     *
     * @param questionSubmitVOQueryRequest 分页查询请求
     * @return 对应数据
     */
    @PostMapping("/page/vo")
    public BaseResponse<Page<QuestionSubmitVO>> pageQueryQuestionSubmitVO(@RequestBody QuestionSubmitVOQueryRequest questionSubmitVOQueryRequest) {
        Page<QuestionSubmitVO> result = questionSubmitService.pageQuestionSubmitVO(questionSubmitVOQueryRequest);
        return BaseResponse.success(result);
    }

    /**
     * 获取提交记录脱敏信息
     *
     * @param id 提交记录id
     * @return 提交记录脱敏信息
     */
    @GetMapping("/get/vo")
    public BaseResponse<QuestionSubmitVO> getQuestionSubmitVOById(Long id) {
        QuestionSubmitVO result = questionSubmitService.getQuestionSubmitVOById(id);
        return BaseResponse.success(result);
    }

    /**
     * 用户最近一次提交记录的代码
     *
     * @param userId     用户id
     * @param questionId 题目id
     * @return 代码
     */
    @GetMapping("/history")
    public BaseResponse<String> getHistoryCode(Long userId, Long questionId) {
        String code = questionSubmitService.getHistoryCode(userId, questionId);
        return BaseResponse.success(code);
    }

    /**
     * 获取用户最近五次提交记录（非Waiting状态）
     *
     * @param userId 用户id
     * @return 最近五次记录
     */
    @GetMapping("/least5")
    public BaseResponse<List<Least5TimeSubmitVO>> getLeast5TimesSubmit(Long userId) {
        List<Least5TimeSubmitVO> result = questionSubmitService.getLeast5TimesSubmit(userId);
        return BaseResponse.success(result);
    }

    /**
     * 删除提交记录
     *
     * @param deleteRequest 删除请求
     * @return 执行信息
     */
    @DeleteMapping
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Boolean> deleteQuestionSubmit(@RequestBody @Valid DeleteRequest deleteRequest) {
        Long id = deleteRequest.getId();
        boolean b = questionSubmitService.removeById(id);
        ThrowUtils.throwIf(!b, ErrorCode.SYSTEM_ERROR, "删除题目提交记录失败 : " + id);
        return BaseResponse.success(true);
    }

    /**
     * 批量删除提交记录
     *
     * @param batchDeleteRequest 删除请求
     * @return 执行信息
     */
    @DeleteMapping("/batchDelete")
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Boolean> batchDeleteQuestionSubmit(@RequestBody @Valid BatchDeleteRequest batchDeleteRequest) {
        boolean b = questionSubmitService.removeBatchByIds(batchDeleteRequest.getIds());
        ThrowUtils.throwIf(!b, ErrorCode.SYSTEM_ERROR, "删除题目提交记录失败 : " + batchDeleteRequest.getIds());
        return BaseResponse.success(true);
    }

    /**
     * 修改题目提交记录
     *
     * @param questionSubmitUpdateRequest 修改提交记录请求
     * @return 执行信息
     */
    @PutMapping
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Boolean> updateQuestionSubmit(@RequestBody @Valid QuestionSubmitUpdateRequest questionSubmitUpdateRequest) {
        questionSubmitService.updateQuestionSubmit(questionSubmitUpdateRequest);
        return BaseResponse.success(true);
    }


    /**
     * 清空提交记录
     *
     * @return 执行信息
     */
    @DeleteMapping("/clean")
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Boolean> cleanQuestionSubmit() {
        questionSubmitMapper.cleanQuestionSubmit();
        return BaseResponse.success(true);
    }

}
